package cn.yitun.chat.yitunserverrunsingleboot;

import cn.yitun.chat.yituncommonutil.YitunPropertiesUtil;
import cn.yitun.chat.yitunserverrunsingleboot.startup.BeanConfig;
import cn.yitun.chat.yitunserverrunsingleboot.startup.DataSourceConfig;
import cn.yitun.chat.yitunserverrunsingleboot.startup.WebConfig;
import cn.yitun.chat.yitunspringcontext.context.annotation.AnnotationBeanFullNameGenerator;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

//import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
//import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
//import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

/**
 * 
 * @author: yitun
 * @date 2018年10月19日 下午4:10:57
 */
@EnableCaching
@EnableAutoConfiguration(exclude = { ErrorMvcAutoConfiguration.class })
@SpringBootApplication()
@MapperScan({"cn.yitun.chat.yitunserverimbase.dao","cn.yitun.chat.yitunserverfilecore.dao"})
@ComponentScan(basePackages = { "cn.yitun.chat" }, nameGenerator = AnnotationBeanFullNameGenerator.class)
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Import({ DataSourceConfig.class, BeanConfig.class, WebConfig.class }) // 导入其他配置
@EnableTransactionManagement
public class BootApplication {
	@Autowired
	private ServerProperties serverProperties;

	public static void main(String[] args) {
		SpringApplication.run(BootApplication.class, args);
	}

	@Bean
	public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
		return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
			@Override
			public void customize(ConfigurableWebServerFactory factory) {
				factory.setPort(serverProperties.getPort());
			}
		};
	}


//	加载yml配置文件，目前使用@PropertySources注解直接装配类
//	@Bean
//	public static PropertySourcesPlaceholderConfigurer properties() {
//		PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
//		YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
//		yaml.setResources(new ClassPathResource("siteuid-config.yml"));
//		configurer.setProperties(yaml.getObject());
//		return configurer;
//	}
}
